#!/bin/bash
#linuxsamu
#
export MINIO_ACCESS_KEY={{ minio_ACCESS_KEY }}
export MINIO_SECRET_KEY={{ minio_SECRET_KEY }}

minio_path={{ minio_base }}/bin
minio_name=minio

{% if groups['minio']| length * minio_path|length >= 4 %}
data_info='{% for path in minio_path %}{{path}} {% endfor %}'
node_info="{% for minioip in groups.minio %}{% for path in minio_path %}http://{{minioip}}:{{minio_port}}{{path}} {% endfor %}{% endfor %}"
{% else %}
data_info='{{minio_path[0]}}'
node_info="${data_info}"
{% endif %}

minio_port={{ minio_port }}
log_dir={{ minio_base }}/logs
START_USER={{ minio_run_user }}

[ -d ${log_dir} ] || mkdir -p ${log_dir}
chown ${START_USER}:${START_USER} ${log_dir} -R
for i in ${data_info};do
    [ -d ${i} ] || mkdir -p ${i}
    chown ${START_USER}:${START_USER} ${i} -R
done


statusfunc()
{
    ps -ef|grep ${minio_name} |grep -v "grep\|sh"
}

startfunc()
{
	su ${START_USER}<<!
    nohup ${minio_path}/${minio_name} server --address 0.0.0.0:${minio_port} ${node_info} > ${log_dir}/minio.log 2>&1 &
!
    echo "启动成功!"
}

stopfunc()
{
    kill `ps -ef|grep minio|grep -v "grep\|sh"|awk '{print $2}'` > /dev/null 2&>1
    echo "关闭成功!"
}
case $1 in 
    start)
    startfunc
    ;;
    stop)
    stopfunc
    ;;
    restart)
    stopfunc
    sleep 1
    startfunc
    ;;
    status)
    statusfunc
    ;;
    *)
    echo "用法:$0 start|stop|restart|status"
esac
